package com.mimiwang.article.mp.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /*插入时自动填充*/
    @Override
    public void insertFill(MetaObject metaObject) {
        Object viewCount = getFieldValByName("viewCount", metaObject);
        if(viewCount==null){
            setFieldValByName("viewCount",0,metaObject);
        }

        Object isTop = getFieldValByName("isTop", metaObject);
        if(isTop==null){
            setFieldValByName("isTop",0,metaObject);
        }

        Object isPublish = getFieldValByName("isPublish", metaObject);
        if(isPublish==null){
            setFieldValByName("isPublish",0,metaObject);
        }

        Date date = new Date();
        Object gmtCreate = getFieldValByName("gmtCreate", metaObject);
        if(gmtCreate==null){
            setFieldValByName("gmtCreate",date,metaObject);
        }

    }

    /*更新时自动填充*/
    @Override
    public void updateFill(MetaObject metaObject) {
        /*更新的时候时间自动注入*/
        Date date = new Date();
        Object gmtModified = getFieldValByName("gmtModified", metaObject);
        if(gmtModified==null){
            setFieldValByName("gmtModified",date,metaObject);
        }
    }
}
